home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / guile-ii.src / guile-ii / guile-src / libguile / numbers.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-17  |  13.0 KB  |  434 lines

  1. /* classes: h_files */
  2.  
  3. #ifndef NUMBERSH
  4. #define NUMBERSH
  5. /*    Copyright (C) 1995 Free Software Foundation, Inc.
  6.  * 
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2, or (at your option)
  10.  * any later version.
  11.  * 
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this software; see the file COPYING.  If not, write to
  19.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  * As a special exception, the Free Software Foundation gives permission
  22.  * for additional uses of the text contained in its release of GUILE.
  23.  *
  24.  * The exception is that, if you link the GUILE library with other files
  25.  * to produce an executable, this does not by itself cause the
  26.  * resulting executable to be covered by the GNU General Public License.
  27.  * Your use of that executable is in no way restricted on account of
  28.  * linking the GUILE library code into it.
  29.  *
  30.  * This exception does not however invalidate any other reasons why
  31.  * the executable file might be covered by the GNU General Public License.
  32.  *
  33.  * This exception applies only to the code released by the
  34.  * Free Software Foundation under the name GUILE.  If you copy
  35.  * code from other Free Software Foundation releases into a copy of
  36.  * GUILE, as the General Public License permits, the exception does
  37.  * not apply to the code that you add in this way.  To avoid misleading
  38.  * anyone as to the status of such modified files, you must delete
  39.  * this exception notice from them.
  40.  *
  41.  * If you write modifications of your own for GUILE, it is your choice
  42.  * whether to permit this exception to apply to your modifications.
  43.  * If you do not wish that, delete this exception notice.  
  44.  */
  45.  
  46.  
  47. #include "__scm.h"
  48.  
  49.  
  50.  
  51.  
  52. /* Immediate Numbers 
  53.  *
  54.  * Inums are exact integer data that fits within an SCM word.
  55.  */
  56. #define SCM_INUMP(x)    (2 & (int)(x))
  57. #define SCM_NINUMP(x)     (!SCM_INUMP(x))
  58.  
  59. #ifdef __TURBOC__
  60. /* shifts of more than one are done by a library call, single shifts are
  61.  * performed in registers
  62.  */
  63. # define SCM_MAKINUM(x) ((((x)<<1)<<1)+2L)
  64. #else
  65. # define SCM_MAKINUM(x) (((x)<<2)+2L)
  66. #endif /* def __TURBOC__ */
  67.  
  68.  
  69. /* SRS is signed right shift */
  70. /* Turbo C++ v1.0 has a bug with right shifts of signed longs!
  71.  * It is believed to be fixed in Turbo C++ v1.01
  72.  */
  73. #if (-1==(((-1)<<2)+2)>>2) && (__TURBOC__ != 0x295)
  74. # define SCM_SRS(x, y) ((x)>>y)
  75. # ifdef __TURBOC__
  76. #  define SCM_INUM(x) (((x)>>1)>>1)
  77. # else
  78. #  define SCM_INUM(x) SCM_SRS(x, 2)
  79. # endif /* def __TURBOC__ */
  80. #else
  81. # define SCM_SRS(x, y) (((x)<0) ? ~((~(x))>>y) : (x)>>y)
  82. # define SCM_INUM(x) SCM_SRS(x, 2)
  83. #endif /*  (-1==(((-1)<<2)+2)>>2) && (__TURBOC__ != 0x295) */
  84.  
  85.  
  86. /* A name for 0.
  87.  */
  88. #define SCM_INUM0 ((SCM) 2)
  89.  
  90.  
  91.  
  92. /* FIXABLE is non-0 if its long argument can be encoded in an INUM.
  93.  */
  94. #define SCM_POSFIXABLE(n) ((n) <= SCM_MOST_POSITIVE_FIXNUM)
  95. #define SCM_NEGFIXABLE(n) ((n) >= SCM_MOST_NEGATIVE_FIXNUM)
  96. #define SCM_UNEGFIXABLE(n) ((n) <= -SCM_MOST_NEGATIVE_FIXNUM)
  97. #define SCM_FIXABLE(n) (SCM_POSFIXABLE(n) && SCM_NEGFIXABLE(n))
  98.  
  99. /* INTBUFLEN is the maximum number of characters neccessary for the
  100.  * printed or scm_string representation of an exact immediate.
  101.  */
  102.  
  103. #ifndef SCM_CHAR_BIT
  104. # define SCM_CHAR_BIT 8
  105. #endif /* ndef SCM_CHAR_BIT */
  106. #ifndef SCM_LONG_BIT
  107. # define SCM_LONG_BIT (SCM_CHAR_BIT*sizeof(long)/sizeof(char))
  108. #endif /* ndef SCM_LONG_BIT */
  109. #define SCM_INTBUFLEN (5+SCM_LONG_BIT)
  110.  
  111. /* FLOBUFLEN is the maximum number of characters neccessary for the
  112.  * printed or scm_string representation of an inexact number.
  113.  */
  114.  
  115. #ifdef FLOATS
  116. # define SCM_FLOBUFLEN (10+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
  117. #endif /* FLOATS */
  118.  
  119.  
  120.  
  121.  
  122. /* Numbers 
  123.  */
  124.  
  125. #ifdef FLOATS
  126. #define SCM_INEXP(x) (SCM_TYP16(x)==scm_tc16_flo)
  127. #define SCM_CPLXP(x) (SCM_CAR(x)==scm_tc_dblc)
  128. #define SCM_REAL(x) (*(((scm_dbl *) (SCM2PTR(x)))->real))
  129. #define SCM_IMAG(x) (*((double *)(CHARS(x)+sizeof(double))))
  130. /* ((&REAL(x))[1]) */
  131. #ifdef SINGLES
  132. #define SCM_REALP(x) ((~REAL_PART & CAR(x))==scm_tc_flo)
  133. #define SCM_SINGP(x) (CAR(x)==scm_tc_flo)
  134. #define SCM_FLO(x) (((scm_flo *)(SCM2PTR(x)))->num)
  135. #define SCM_REALPART(x) (SCM_SINGP(x)?0.0+FLO(x):SCM_REAL(x))
  136. #else /* SINGLES */
  137. #define SCM_REALP(x) (CAR(x)==scm_tc_dblr)
  138. #define SCM_REALPART SCM_REAL
  139. #endif /* SINGLES */
  140. #endif
  141.  
  142.  
  143. /* Define BIGDIG to an integer type whose size is smaller than long if
  144.  * you want bignums.  BIGRAD is one greater than the biggest BIGDIG. 
  145.  *
  146.  * Define DIGSTOOBIG if the digits equivalent to a long won't fit in a long. 
  147.  */
  148. #ifdef BIGNUMS
  149. # ifdef _UNICOS
  150. #  define SCM_DIGSTOOBIG
  151. #  if (1L << 31) <= SCM_USHRT_MAX
  152. #   define SCM_BIGDIG unsigned short
  153. #  else
  154. #   define SCM_BIGDIG unsigned int
  155. #  endif /*  (1L << 31) <= USHRT_MAX */
  156. #  define SCM_BITSPERDIG 32
  157. # else
  158. #  define SCM_BIGDIG unsigned short
  159. #  define SCM_BITSPERDIG (sizeof(SCM_BIGDIG)*SCM_CHAR_BIT)
  160. # endif /* def _UNICOS */
  161. # define SCM_BIGRAD (1L << SCM_BITSPERDIG)
  162. # define SCM_DIGSPERLONG ((sizet)((sizeof(long)*SCM_CHAR_BIT+SCM_BITSPERDIG-1)/SCM_BITSPERDIG))
  163. # define SCM_BIGUP(x) ((unsigned long)(x) << SCM_BITSPERDIG)
  164. # define SCM_BIGDN(x) ((x) >> SCM_BITSPERDIG)
  165. # define SCM_BIGLO(x) ((x) & (SCM_BIGRAD-1))
  166. #endif /* def BIGNUMS */
  167.  
  168. #ifndef SCM_BIGDIG
  169. /* Definition is not really used but helps various function
  170.  * prototypes to compile with conditionalization.
  171.  */
  172. # define SCM_BIGDIG unsigned short
  173. # define NO_BIGDIG
  174. # ifndef FLOATS
  175. #  define INUMS_ONLY
  176. # endif /* ndef FLOATS */
  177. #endif /* ndef SCM_BIGDIG */
  178.  
  179. #ifdef FLOATS
  180. #define SCM_NUMBERP(x) (INUMP(x) || (SCM_NIMP(x) && NUMP(x)))
  181. #else
  182. #ifdef BIGDIG
  183. #define SCM_NUMBERP(x) (INUMP(x) || (SCM_NIMP(x) && NUMP(x)))
  184. #else
  185. #define SCM_NUMBERP INUMP
  186. #endif
  187. #endif
  188. #define SCM_NUMP(x) ((0xfcff & (int)CAR(x))==tc7_smob)
  189. #define SCM_BIGP(x) (TYP16S(x)==tc16_bigpos)
  190. #define SCM_BIGSIGN(x) (0x0100 & (int)CAR(x))
  191. #define SCM_BDIGITS(x) ((BIGDIG *)(CDR(x)))
  192. #define SCM_NUMDIGS(x) ((sizet)(CAR(x)>>16))
  193. #define SCM_SETNUMDIGS(x, v, t) CAR(x) = (((v)+0L)<<16)+(t)
  194.  
  195.  
  196. #ifdef FLOATS
  197. typedef struct scm_dblproc
  198. {
  199.   char *scm_string;
  200.   double (*cproc) ();
  201. } scm_dblproc;
  202.  
  203. #ifdef SINGLES
  204. typedef struct scm_flo
  205. {
  206.   SCM type;
  207.   float num;
  208. } scm_flo;
  209. #endif
  210.  
  211. typedef struct scm_dbl
  212. {
  213.   SCM type;
  214.   double *real;
  215. } scm_dbl;
  216. #endif
  217.  
  218.  
  219.  
  220.  
  221. #ifdef __STDC__
  222. extern SCM scm_exact_p(SCM x);
  223. extern SCM scm_odd_p(SCM n);
  224. extern SCM scm_even_p(SCM n);
  225. extern SCM scm_abs(SCM x);
  226. extern SCM scm_quotient(SCM x, SCM y);
  227. extern SCM scm_remainder(SCM x, SCM y);
  228. extern SCM scm_modulo(SCM x, SCM y);
  229. extern SCM scm_gcd(SCM x, SCM y);
  230. extern SCM scm_lcm(SCM n1, SCM n2);
  231. extern SCM scm_logand(SCM n1, SCM n2);
  232. extern SCM scm_logior(SCM n1, SCM n2);
  233. extern SCM scm_logxor(SCM n1, SCM n2);
  234. extern SCM scm_logtest(SCM n1, SCM n2);
  235. extern SCM scm_logbit_p(SCM n1, SCM n2);
  236. extern SCM scm_logand(SCM n1, SCM n2);
  237. extern SCM scm_logior(SCM n1, SCM n2);
  238. extern SCM scm_logxor(SCM n1, SCM n2);
  239. extern SCM scm_logtest(SCM n1, SCM n2);
  240. extern SCM scm_logbit_p(SCM n1, SCM n2);
  241. extern SCM scm_lognot(SCM n);
  242. extern SCM scm_integer_expt(SCM z1, SCM z2);
  243. extern SCM scm_ash(SCM n, SCM cnt);
  244. extern SCM scm_bit_extract(SCM n, SCM start, SCM end);
  245. extern SCM scm_logcount (SCM n);
  246. extern SCM scm_integer_length(SCM n);
  247. extern SCM scm_mkbig(sizet nlen, int sign);
  248. extern SCM scm_big2inum(SCM b, sizet l);
  249. extern SCM scm_adjbig(SCM b, sizet nlen);
  250. extern SCM scm_normbig(SCM b);
  251. extern SCM scm_copybig(SCM b, int sign);
  252. extern SCM scm_long2big(long n);
  253. extern SCM scm_2ulong2big(unsigned long * np);
  254. extern SCM scm_ulong2big(unsigned long n);
  255. extern int scm_bigcomp(SCM x, SCM y);
  256. extern void scm_longdigs(long x, SCM_BIGDIG digs[]);
  257. extern SCM scm_addbig(SCM_BIGDIG *x, sizet nx, int xsgn, SCM bigy, int sgny);
  258. extern SCM scm_mulbig(SCM_BIGDIG *x, sizet nx, SCM_BIGDIG *y, sizet ny, int sgn);
  259. extern unsigned int scm_divbigdig(SCM_BIGDIG *ds, sizet h, SCM_BIGDIG div);
  260. extern SCM scm_divbigint(SCM x, long z, int sgn, int mode);
  261. extern SCM scm_divbigbig(SCM_BIGDIG *x, sizet nx, SCM_BIGDIG *y, sizet ny, int sgn, int modes);
  262. extern sizet scm_iint2str(long num, int rad, char *p);
  263. extern SCM scm_number_to_string(SCM x, SCM radix);
  264. extern int scm_floprint(SCM sexp, SCM port, int writing);
  265. extern int scm_bigprint(SCM exp, SCM port, int writing);
  266. extern SCM scm_istr2int(char *str, long len, long radix);
  267. extern SCM scm_istr2int(char *str, long len, long radix);
  268. extern SCM scm_istr2flo(char *str, long len, long radix);
  269. extern SCM scm_istring2number(char *str, long len, long radix);
  270. extern SCM scm_string_to_number(SCM str, SCM radix);
  271. extern SCM scm_makdbl (double x, double y);
  272. extern SCM scm_bigequal(SCM x, SCM y);
  273. extern SCM scm_floequal(SCM x, SCM y);
  274. extern SCM scm_number_p(SCM x);
  275. extern SCM scm_real_p(SCM x);
  276. extern SCM scm_int_p(SCM x);
  277. extern SCM scm_inexact_p(SCM x);
  278. extern SCM scm_num_eq_p(SCM x, SCM y);
  279. extern SCM scm_less_p(SCM x, SCM y);
  280. extern SCM scm_gr_p(SCM x, SCM y);
  281. extern SCM scm_leq_p(SCM x, SCM y);
  282. extern SCM scm_geq_p(SCM x, SCM y);
  283. extern SCM scm_zero_p(SCM z);
  284. extern SCM scm_positive_p(SCM x);
  285. extern SCM scm_negative_p(SCM x);
  286. extern SCM scm_max(SCM x, SCM y);
  287. extern SCM scm_min(SCM x, SCM y);
  288. extern SCM scm_sum(SCM x, SCM y);
  289. extern SCM scm_difference(SCM x, SCM y);
  290. extern SCM scm_product(SCM x, SCM y);
  291. extern double scm_num2dbl (SCM a, char * why);
  292. extern SCM scm_fuck (SCM a);
  293. extern SCM scm_divide(SCM x, SCM y);
  294. extern double scm_asinh(double x);
  295. extern double scm_acosh(double x);
  296. extern double scm_atanh(double x);
  297. extern double scm_truncate(double x);
  298. extern double scm_round(double x);
  299. extern double scm_exact_to_inexact(double z);
  300. extern SCM scm_sys_expt(SCM z1, SCM z2);
  301. extern SCM scm_sys_atan2(SCM z1, SCM z2);
  302. extern SCM scm_make_rectangular(SCM z1, SCM z2);
  303. extern SCM scm_make_polar(SCM z1, SCM z2);
  304. extern SCM scm_realpart(SCM z);
  305. extern SCM scm_imag_part(SCM z);
  306. extern SCM scm_magnitude(SCM z);
  307. extern SCM scm_angle(SCM z);
  308. extern SCM scm_inexact_to_exact(SCM z);
  309. extern SCM scm_trunc(SCM x);
  310. extern SCM scm_dbl2big(double d);
  311. extern double scm_big2dbl(SCM b);
  312. extern SCM scm_long2num(long sl);
  313. extern SCM scm_ulong2num(unsigned long sl);
  314. extern long scm_num2long(SCM num, char *pos, char *s_caller);
  315. extern long num2long(SCM num, char *pos, char *s_caller);
  316. extern unsigned long scm_num2ulong(SCM num, char *pos, char *s_caller);
  317. extern void scm_init_numbers (void);
  318.  
  319. #else /* STDC */
  320. extern SCM scm_exact_p();
  321. extern SCM scm_odd_p();
  322. extern SCM scm_even_p();
  323. extern SCM scm_abs();
  324. extern SCM scm_quotient();
  325. extern SCM scm_remainder();
  326. extern SCM scm_modulo();
  327. extern SCM scm_gcd();
  328. extern SCM scm_lcm();
  329. extern SCM scm_logand();
  330. extern SCM scm_logior();
  331. extern SCM scm_logxor();
  332. extern SCM scm_logtest();
  333. extern SCM scm_logbit_p();
  334. extern SCM scm_logand();
  335. extern SCM scm_logior();
  336. extern SCM scm_logxor();
  337. extern SCM scm_logtest();
  338. extern SCM scm_logbit_p();
  339. extern SCM scm_lognot();
  340. extern SCM scm_integer_expt();
  341. extern SCM scm_ash();
  342. extern SCM scm_bit_extract();
  343. extern SCM scm_logcount ();
  344. extern SCM scm_integer_length();
  345. extern SCM scm_mkbig();
  346. extern SCM scm_big2inum();
  347. extern SCM scm_adjbig();
  348. extern SCM scm_normbig();
  349. extern SCM scm_copybig();
  350. extern SCM scm_long2big();
  351. extern SCM scm_2ulong2big();
  352. extern SCM scm_ulong2big();
  353. extern int scm_bigcomp();
  354. extern void scm_longdigs();
  355. extern SCM scm_addbig();
  356. extern SCM scm_mulbig();
  357. extern unsigned int scm_divbigdig();
  358. extern SCM scm_divbigint();
  359. extern SCM scm_divbigbig();
  360. extern sizet scm_iint2str();
  361. extern SCM scm_number_to_string();
  362. extern int scm_floprint();
  363. extern int scm_bigprint();
  364. extern SCM scm_istr2int();
  365. extern SCM scm_istr2int();
  366. extern SCM scm_istr2flo();
  367. extern SCM scm_istring2number();
  368. extern SCM scm_string_to_number();
  369. extern SCM scm_makdbl ();
  370. extern SCM scm_bigequal();
  371. extern SCM scm_floequal();
  372. extern SCM scm_number_p();
  373. extern SCM scm_real_p();
  374. extern SCM scm_int_p();
  375. extern SCM scm_inexact_p();
  376. extern SCM scm_num_eq_p();
  377. extern SCM scm_less_p();
  378. extern SCM scm_gr_p();
  379. extern SCM scm_leq_p();
  380. extern SCM scm_geq_p();
  381. extern SCM scm_zero_p();
  382. extern SCM scm_positive_p();
  383. extern SCM scm_negative_p();
  384. extern SCM scm_max();
  385. extern SCM scm_min();
  386. extern SCM scm_sum();
  387. extern SCM scm_difference();
  388. extern SCM scm_product();
  389. extern double scm_num2dbl ();
  390. extern SCM scm_fuck ();
  391. extern SCM scm_divide();
  392. extern double scm_asinh();
  393. extern double scm_acosh();
  394. extern double scm_atanh();
  395. extern double scm_truncate();
  396. extern double scm_round();
  397. extern double scm_exact_to_inexact();
  398. extern SCM scm_sys_expt();
  399. extern SCM scm_sys_atan2();
  400. extern SCM scm_make_rectangular();
  401. extern SCM scm_make_polar();
  402. extern SCM scm_realpart();
  403. extern SCM scm_imag_part();
  404. extern SCM scm_magnitude();
  405. extern SCM scm_angle();
  406. extern SCM scm_inexact_to_exact();
  407. extern SCM scm_trunc();
  408. extern SCM scm_dbl2big();
  409. extern double scm_big2dbl();
  410. extern SCM scm_long2num();
  411. extern SCM scm_ulong2num();
  412. extern long scm_num2long();
  413. extern long num2long();
  414. extern unsigned long scm_num2ulong();
  415. extern void scm_init_numbers ();
  416.  
  417. #endif /* STDC */
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433. #endif  /* NUMBERSH */
  434.